package org.netbeans.modules.qcubedpalette;

import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import org.openide.text.ActiveEditorDrop;

/**
 *	QControl
 *	Base class to handle generic functionality for all QControls.
 */
public abstract class QControl implements ActiveEditorDrop {

	protected String name;

	public QControl() {
	}

	public void setName(String name) {
		this.name = name;
	}

	protected abstract String createMethod();

	protected abstract String createMethodCall();

	protected abstract String createDataMember();

	protected abstract QControlCustomizer getCustomizer();

	@Override
	public boolean handleTransfer(JTextComponent targetComponent) {
		boolean accept = this.getCustomizer().showDialog();

		if (accept) {
			try {
				String method = this.createMethod();
				String dataMember = this.createDataMember();
				String methodCall = this.createMethodCall();
				PaletteUtilities.insertAt(method, targetComponent, targetComponent.getCaret().getDot());
				PaletteUtilities.insertAt(dataMember, targetComponent, PaletteUtilities.findClassMemberInsertionOffset(targetComponent.getDocument()));
				PaletteUtilities.insertAt(methodCall, targetComponent, PaletteUtilities.findFormCreateInsertionOffset(targetComponent.getDocument()));
			} catch (BadLocationException ble) {
				accept = false;
			}
		}
		return accept;
	}

}
